package org.hawk.gwt.ppc.utils;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringWriter;

import junit.framework.TestCase;

import org.apache.tools.ant.filters.StringInputStream;

public class FileSystemTestCase extends TestCase {
	public void testMkdir(){
		assertFalse(new File("./bin/mydir").isDirectory());
		File dir = FileSystem.mkdir("./bin/mydir");
		assertNotNull(dir);
		assertTrue(dir.isDirectory());
		dir.delete();
	}
	public void testRrm() {
		File dir = FileSystem.mkdir("./bin/mydir/this/is/long/path");
		assertNotNull(dir);
		assertTrue(dir.isDirectory());
		FileSystem.rm("./bin/mydir");
		assertFalse(dir.exists());
		assertFalse(new File("./bin/mydir").exists());
	}
	
	public void testWrite() throws IOException {
		File file = new File("./bin/myfile.txt");
		assertFalse(file.exists());
		FileSystem.write(file, new StringInputStream("this is the file"));
		assertTrue(file.isFile());
		StringWriter check = new StringWriter();
		Reader reader = new FileReader(file);
		char[] buf = new char[1024];
		int len = 0;
		while((len=reader.read(buf))>0){
			check.write(buf,0,len);
		}
		reader.close();
		assertEquals("this is the file", check.toString());
		FileSystem.rm(file);
		assertFalse(file.exists());
	}
}
